<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="王诗翔" />



<meta name="description" content="来源： Linux命令行与shell脚本编程大全 内容：   创建文本菜单 创建文本窗口部件 添加X Window图形   创建文本菜单创建交互式shell脚本最常用的方法是使用菜单，它提供了各种选项帮助脚本用户了解脚本能做到的和不能做的。 shell脚本菜单的核心是case命令，该命令会根据用户在菜单上的选择来执行特定命令。 下面我们逐步了解和创建基于菜单的shell脚本的步骤。">
<meta name="keywords" content="shell笔记,linux">
<meta property="og:type" content="article">
<meta property="og:title" content="使用shell创建文本菜单和窗口部件">
<meta property="og:url" content="https://shixiangwang.github.io/2017/11/29/shell-create-text-menu-and-window/index.html">
<meta property="og:site_name" content="Cloudberry">
<meta property="og:description" content="来源： Linux命令行与shell脚本编程大全 内容：   创建文本菜单 创建文本窗口部件 添加X Window图形   创建文本菜单创建交互式shell脚本最常用的方法是使用菜单，它提供了各种选项帮助脚本用户了解脚本能做到的和不能做的。 shell脚本菜单的核心是case命令，该命令会根据用户在菜单上的选择来执行特定命令。 下面我们逐步了解和创建基于菜单的shell脚本的步骤。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-01-27T04:08:47.822Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用shell创建文本菜单和窗口部件">
<meta name="twitter:description" content="来源： Linux命令行与shell脚本编程大全 内容：   创建文本菜单 创建文本窗口部件 添加X Window图形   创建文本菜单创建交互式shell脚本最常用的方法是使用菜单，它提供了各种选项帮助脚本用户了解脚本能做到的和不能做的。 shell脚本菜单的核心是case命令，该命令会根据用户在菜单上的选择来执行特定命令。 下面我们逐步了解和创建基于菜单的shell脚本的步骤。">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="Cloudberry" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="../../../../css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>使用shell创建文本菜单和窗口部件 | Cloudberry</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">王诗翔</a></h1>
        </hgroup>

        
        <p class="header-subtitle">A site for writing life</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="../../../../index.html">主页</a></li>
                        
                            <li><a href="../../../../archives/">所有文章</a></li>
                        
                            <li><a href="../../../../tags/">标签云</a></li>
                        
                            <li><a href="../../../../about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:w_shixiang@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/ShixiangWang" title="GitHub"></a>
                            
                                <a class="fa RSS" href="../../../../atom.xml" title="RSS"></a>
                            
                                <a class="fa 知乎" href="https://www.zhihu.com/people/wang-shi-xiang-65/activities" title="知乎"></a>
                            
                                <a class="fa 简书" href="https://www.jianshu.com/u/b6608e27dc74" title="简书"></a>
                            
                                <a class="fa Twitter" href="https://twitter.com/WangShxiang" title="Twitter"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Gist/">Gist</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Linux-shell/">Linux shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/OOP/">OOP</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/R/">R</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rmarkdown/">Rmarkdown</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rmd/">Rmd</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rstudio/">Rstudio</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/bash-shell/">bash shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/fasta/">fasta</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/git/">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/github/">github</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/lapply/">lapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/markdown/">markdown</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/md/">md</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/sapply/">sapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/sed/">sed</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/shell笔记/">shell笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/survival/">survival</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/survminer/">survminer</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/vapply/">vapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/写作/">写作</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/数据处理/">数据处理</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/文本处理/">文本处理</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/生存分析/">生存分析</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/生物信息学/">生物信息学</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/类/">类</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/迭代计算/">迭代计算</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.ruanyifeng.com/blog/archives.html">阮一峰的网络日志</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://blog.fens.me/series-r/">R的极客理想系列</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://yihui.name/">谢益辉</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.huangshujia.me/">解螺旋的矿工</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://github.com/GuangchuangYu">GuangchuangYu</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://vip.biotrainee.com/">生信技能树</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">人该是自己生活的主宰，而不是别人手中的行货</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">王诗翔</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">王诗翔</a></h1>
            </hgroup>
            
            <p class="header-subtitle">A site for writing life</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="../../../../index.html">主页</a></li>
                
                    <li><a href="../../../../archives/">所有文章</a></li>
                
                    <li><a href="../../../../tags/">标签云</a></li>
                
                    <li><a href="../../../../about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:w_shixiang@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/ShixiangWang" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="../../../../atom.xml" title="RSS"></a>
                            
                                <a class="fa 知乎" target="_blank" href="https://www.zhihu.com/people/wang-shi-xiang-65/activities" title="知乎"></a>
                            
                                <a class="fa 简书" target="_blank" href="https://www.jianshu.com/u/b6608e27dc74" title="简书"></a>
                            
                                <a class="fa Twitter" target="_blank" href="https://twitter.com/WangShxiang" title="Twitter"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-shell-create-text-menu-and-window" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="" class="article-date">
      <time datetime="2017-11-28T16:00:00.000Z" itemprop="datePublished">2017-11-29</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      使用shell创建文本菜单和窗口部件
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="../../../../categories/Linux杂烩/">Linux杂烩</a><a class="article-category-link" href="../../../../categories/Linux杂烩/shell编程/">shell编程</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="../../../../tags/linux/">linux</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="../../../../tags/shell笔记/">shell笔记</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><em>来源： Linux命令行与shell脚本编程大全</em></p>
<p>内容：</p>
<blockquote>
<ul>
<li>创建文本菜单</li>
<li>创建文本窗口部件</li>
<li>添加X Window图形</li>
</ul>
</blockquote>
<h2 id="创建文本菜单"><a href="#创建文本菜单" class="headerlink" title="创建文本菜单"></a>创建文本菜单</h2><p>创建交互式shell脚本最常用的方法是使用菜单，它提供了各种选项帮助脚本用户了解脚本能做到的和不能做的。</p>
<p>shell脚本菜单的核心是<code>case</code>命令，该命令会根据用户在菜单上的选择来执行特定命令。</p>
<p>下面我们逐步了解和创建基于菜单的shell脚本的步骤。</p>
<a id="more"></a>
<h3 id="创建菜单布局"><a href="#创建菜单布局" class="headerlink" title="创建菜单布局"></a>创建菜单布局</h3><p><strong>第一步</strong>是决定在菜单上显示哪些元素以及想要显示的布局方式。</p>
<p><strong>在创建菜单前，通常先清空显示器上已有的内容。这样能在干净的，没有干扰的环境中显示菜单了。</strong></p>
<p><code>clear</code>命令使用当前终端的<code>terminfo</code>数据来清理出现在屏幕上的文字。运行<code>clear</code>命令后可以使用<code>echo</code>命令显示菜单元素。</p>
<p><strong>默认，echo命令只显示可打印的文本字符。</strong>而在创建菜单时一些非文本字符也非常有用，比如制表符和换行符。我们需要添加<code>-e</code>选项使得<code>echo</code>命令能解析包含在其中的非文本字符。</p>
<p>例如，</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ echo -e "1.\tDisplay disk space"</span><br><span class="line">1.	Display disk space</span><br></pre></td></tr></table></figure>
<p>这对于格式化菜单项布局非常方便，只需要几个<code>echo</code>命令就可以创建一个还不错的菜单。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">clear</span><br><span class="line">echo</span><br><span class="line">echo -e "\t\t\tSys Admin Menu\n"</span><br><span class="line">echo -e "\t1. Display disk space"</span><br><span class="line">echo -e "\t2. Display logged on users"</span><br><span class="line">echo -e "\t3. Display memory usage"</span><br><span class="line">echo -e "\t0. Exit menu\n\n"</span><br><span class="line">echo -en "\t\tEnter an option: "</span><br></pre></td></tr></table></figure>
<p>最后一行<code>-en</code>选项去掉末尾换行符使得菜单更专业点，光标会在行尾等待用户输入。</p>
<p><strong>创建菜单的最后一步是获取用户输入。</strong>这一步用<code>read</code>命令。因为我们只期望用户使用单字符输入，在命令加<code>-n</code>选项进行限定。这样用户只需要输入一个数字，不用摁回车键。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">read -n 1 option</span><br></pre></td></tr></table></figure>
<h3 id="创建菜单函数"><a href="#创建菜单函数" class="headerlink" title="创建菜单函数"></a>创建菜单函数</h3><p>shell脚本菜单选项作为一组独立的函数实现起来更为容易。要做到这一点，你要为每个菜单项创建独立的shell函数。<strong>第一步</strong>是决定你希望脚本执行那些功能，然后将这些功能以函数的形式放在代码中。</p>
<p><strong>通常我们会为还没有实现的函数先创建一个<em>桩函数</em>，它是一个控函数，或者只有一个echo语句，说明最终这里需要什么内容。</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function diskspace &#123;</span><br><span class="line">  clear</span><br><span class="line">  echo "This is where the diskspace commands will do"</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这允许菜单在我实现某个函数时仍然能正常操作。不需要我们写出所有函数之后才能让菜单投入使用。函数从<code>clear</code>命令开始，这样我们就能在一个干净的屏幕上执行该函数，不会收到原先菜单的干扰。</p>
<p><strong>另外，将菜单布局本身作为一个函数来创建有利于菜单制作。</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">function menu &#123;</span><br><span class="line">  	clear</span><br><span class="line">trueecho</span><br><span class="line">trueecho -e "\t\t\tSys Admin Menu\n"</span><br><span class="line">trueecho -e "\t1. Display disk space"</span><br><span class="line">trueecho -e "\t2. Display logged on users"</span><br><span class="line">trueecho -e "\t3. Display memory usage"</span><br><span class="line">trueecho -e "\t0. Exit menu\n\n"</span><br><span class="line">trueecho -en "\t\tEnter an option: "</span><br><span class="line">trueread -n 1 option</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样我们能在任何时候调用该函数以此重现菜单。</p>
<h3 id="添加菜单逻辑"><a href="#添加菜单逻辑" class="headerlink" title="添加菜单逻辑"></a>添加菜单逻辑</h3><p>下一步我们需要创建程序逻辑将菜单布局和函数结合起来。这需要使用<code>case</code>命令。</p>
<p><code>case</code>命令应该根据菜单中输入的字符来调用相应的函数，用case命令字符星号来处理所有不正确的菜单项。</p>
<p>下面展示了典型菜单的<code>case</code>用法：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">menu</span><br><span class="line">case $option in</span><br><span class="line">0)</span><br><span class="line">truebreak ;;</span><br><span class="line">1)</span><br><span class="line">truediskspace ;;</span><br><span class="line">2)</span><br><span class="line">truewhoseon ;;</span><br><span class="line">3)</span><br><span class="line">truememusage ;;</span><br><span class="line">*)</span><br><span class="line">trueclear</span><br><span class="line">trueecho "Sorry, wrong selection";;</span><br><span class="line">esac</span><br></pre></td></tr></table></figure>
<p>这里首先调用<code>menu</code>函数清空屏幕并显示菜单。<code>menu</code>函数中的<code>read</code>命令会一直等待，知道用户在键盘上键入一个字符。然后<code>case</code>命令会接管余下的处理过程，基于字符调用相应的函数。</p>
<h3 id="整合shell脚本菜单"><a href="#整合shell脚本菜单" class="headerlink" title="整合shell脚本菜单"></a>整合shell脚本菜单</h3><p>现在让我们将前面的步骤全部组合起来，看看它们是如何协作的。</p>
<p>这是一个完整的菜单脚本例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ cat test14</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> simple script menu</span></span><br><span class="line"></span><br><span class="line">function diskspace &#123;</span><br><span class="line">trueclear</span><br><span class="line">truedf -k</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function whoseon &#123;</span><br><span class="line">trueclear</span><br><span class="line">truewho</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function memusage &#123;</span><br><span class="line">trueclear</span><br><span class="line">truecat /proc/meminfo</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function menu &#123;</span><br><span class="line">trueclear</span><br><span class="line">trueecho</span><br><span class="line">trueecho -e "\t\t\tSys Admin Menu\n"</span><br><span class="line">trueecho -e "\t1. Display disk space"</span><br><span class="line">trueecho -e "\t2. Display logged on users"</span><br><span class="line">trueecho -e "\t3. Display memory usage"</span><br><span class="line">trueecho -e "\t0. Exit menu\n\n"</span><br><span class="line">trueecho -en "\t\tEnter an option: "</span><br><span class="line">trueread -n 1 option</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">while [ 1 ]</span><br><span class="line">do</span><br><span class="line">truemenu</span><br><span class="line">truecase $option in</span><br><span class="line">true0)</span><br><span class="line">truetruebreak ;;</span><br><span class="line">true1)</span><br><span class="line">truetruediskspace ;;</span><br><span class="line">true2)</span><br><span class="line">truetruewhoseon ;;</span><br><span class="line">true3)</span><br><span class="line">truetruememusage ;;</span><br><span class="line">true*)</span><br><span class="line">truetrueclear</span><br><span class="line">truetrueecho "Sorry, wrong selection" ;;</span><br><span class="line">trueesac</span><br><span class="line">trueecho -en "\n\n\t\t\tHit any key to continue"</span><br><span class="line">trueread -n 1 line</span><br><span class="line">done</span><br><span class="line">clear</span><br></pre></td></tr></table></figure>
<p>使用：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">truetrueSys Admin Menu</span><br><span class="line"></span><br><span class="line">1. Display disk space</span><br><span class="line">2. Display logged on users</span><br><span class="line">3. Display memory usage</span><br><span class="line">0. Exit menu</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">trueEnter an option:</span><br></pre></td></tr></table></figure>
<p>输入1：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">文件系统           1K-块     已用      可用 已用% 挂载点</span><br><span class="line">udev             4006080        0   4006080    0% /dev</span><br><span class="line">tmpfs             807220    81004    726216   11% /run</span><br><span class="line">/dev/sda4      305650672 14226064 275828680    5% /</span><br><span class="line">tmpfs            4036100     1724   4034376    1% /dev/shm</span><br><span class="line">tmpfs               5120        4      5116    1% /run/lock</span><br><span class="line">tmpfs            4036100        0   4036100    0% /sys/fs/cgroup</span><br><span class="line">/dev/sda3         524272     4684    519588    1% /boot/efi</span><br><span class="line">tmpfs             807220       52    807168    1% /run/user/1000</span><br><span class="line">tmpfs             807220       16    807204    1% /run/user/125</span><br><span class="line">/dev/sda2      421886972 23340376 398546596    6% /media/wsx/存储</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">truetruetrueHit any key to continue</span><br></pre></td></tr></table></figure>
<p>其他都可以自己测试一下，我就不赘言了。</p>
<h3 id="使用select命令"><a href="#使用select命令" class="headerlink" title="使用select命令"></a>使用select命令</h3><p><code>select</code>命令只需要一条命令就可以创建出菜单，然后获取输入的答案并自动处理。</p>
<p>命令格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">select variable in list</span><br><span class="line">do</span><br><span class="line">truecommands</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p><strong><code>list</code>参数是由空格分隔的文本选项列表，这些列表构成了整个菜单。</strong><code>select</code>命令会将每个列表项显示成一个带编号的选项，然后为选项显示一个由<code>PS3</code>环境变量定义的特殊提示符。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ cat smenu1</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> using select <span class="keyword">in</span> the menu</span></span><br><span class="line"></span><br><span class="line">function diskspace &#123;</span><br><span class="line">trueclear</span><br><span class="line">truedf -k</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function whoseon &#123;</span><br><span class="line">trueclear</span><br><span class="line">truewho</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function memusage &#123;</span><br><span class="line">trueclear</span><br><span class="line">truecat /proc/meminfo</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PS3="Enter an option: "</span><br><span class="line">select option in "Display disk space" "Display logged on users" "Display memory usage" "Exit program"</span><br><span class="line">do</span><br><span class="line">truecase $option in</span><br><span class="line">true"Exit program")</span><br><span class="line">truetruebreak ;;</span><br><span class="line">true"Display disk space")</span><br><span class="line">truetruediskspace ;;</span><br><span class="line">true"Display logged on users")</span><br><span class="line">truetruememusage ;;</span><br><span class="line">true"Display memory usage"）</span><br><span class="line">truetruememusage ;;</span><br><span class="line">true*)</span><br><span class="line">truetrueclear</span><br><span class="line">truetrueecho "Sorry, wrong selection";;</span><br><span class="line">trueesac</span><br><span class="line">done</span><br><span class="line">clear</span><br></pre></td></tr></table></figure>
<p>运行会自动生成如下菜单项：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ ./smenu1</span><br><span class="line">1) Display disk space	    3) Display memory usage</span><br><span class="line">2) Display logged on users  4) Exit program</span><br><span class="line">Enter an option:</span><br></pre></td></tr></table></figure>
<p><strong>在使用<code>select</code>命令时，记住存储在变量中的结果值是整个文本字符串而不是跟菜单项相关联的数字。文本字符串是要在<code>case</code>语句中比较的内容。</strong></p>
<h2 id="制作窗口"><a href="#制作窗口" class="headerlink" title="制作窗口"></a>制作窗口</h2><p><code>dialog</code>包能够用ANSI转义控制字符在文本环境中创建标准的窗口对话框。我们可以将这些对话框融入自己的shell脚本中，借此与用户进行交互。这部分我们来学习如何使用<code>dialog</code>包。</p>
<p>安装：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install dialog</span><br></pre></td></tr></table></figure>
<h3 id="dialog包"><a href="#dialog包" class="headerlink" title="dialog包"></a>dialog包</h3><p><code>dialog</code>包使用命令行参数来决定生成哪种窗口部件（widget）。部件是dialog包中窗口元素的术语。</p>
<table>
<thead>
<tr>
<th>部件</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>calendar</td>
<td>提供选择日期的日历</td>
</tr>
<tr>
<td>checklist</td>
<td>显示多个选项（其中每个选项都能打开或关闭）</td>
</tr>
<tr>
<td>form</td>
<td>构建一个带有标签以及文本字段（可以填写内容）的表单</td>
</tr>
<tr>
<td>fselect</td>
<td>提供一个文件选择窗口来浏览选择文件</td>
</tr>
<tr>
<td>gauge</td>
<td>显示完成的百分比进度条</td>
</tr>
<tr>
<td>infobox</td>
<td>显示一条消息，但不用等待回应</td>
</tr>
<tr>
<td>inputbox</td>
<td>提供一个输入文本用的文本表单</td>
</tr>
<tr>
<td>inputmenu</td>
<td>提供一个可编辑的菜单</td>
</tr>
<tr>
<td>menu</td>
<td>显示可选择的一系列选项</td>
</tr>
<tr>
<td>msgbox</td>
<td>显示一条消息，并要求用户选择OK按钮</td>
</tr>
<tr>
<td>pause</td>
<td>显示一个进度条来显示暂定期间的状态</td>
</tr>
<tr>
<td>passwordbox</td>
<td>显示一个文本框，但会隐藏输入的文本</td>
</tr>
<tr>
<td>passwordform</td>
<td>显示一个带标签的隐藏文本字段的表单</td>
</tr>
<tr>
<td>radiolist</td>
<td>提供一组菜单选项，但只能选择其中一个</td>
</tr>
<tr>
<td>tailbox</td>
<td>用tail命令在滚动窗口中显示文件的内容</td>
</tr>
<tr>
<td>tailboxbg</td>
<td>跟tailbox一样，但是在后台模式中运行</td>
</tr>
<tr>
<td>textbox</td>
<td>在滚动窗口中显示文件的内容</td>
</tr>
<tr>
<td>timebox</td>
<td>提供一个选择小时、分钟和秒数的窗口</td>
</tr>
<tr>
<td>yesno</td>
<td>提供一条带有Yes和No按钮的简单消息</td>
</tr>
</tbody>
</table>
<p>如上表所见，我们可以选择很多不同的部件。只需要多花点功夫就可以让脚本看起来更专业。</p>
<p><strong>要在命令行上指定某个特定的部件，需要使用双破折线格式</strong>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dialog --widget parameters</span><br></pre></td></tr></table></figure>
<p>其中<code>widget</code>是上表中某个特定的部件，<code>parameters</code>定义了部件窗口的大小以及部件需要的文本。</p>
<p><strong>每个dialog部件都提供了两种形式的输出：</strong></p>
<ul>
<li>使用STDERR</li>
<li>使用退出状态码</li>
</ul>
<p>可以通过<code>dialog</code>命令的退出状态码来确定用户选择的按钮。如果选择了Yes或OK按钮，命令会返回状态码<code>0</code>。如果选择了Cancer或No按钮，命令会返回状态码<code>1</code>。可用标准的<code>$?</code>变量来确定<code>dialog</code>部件具体选择了哪个按钮。</p>
<p>如果部件返回了数据，<code>dialog</code>命令会将数据发送到STDERR。我们可以用标准的bash shell方法将其重定向到另一个文件或文件描述符中。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dialog --inputbox "Enter your age:" 10 20 2&gt;age.txt</span><br></pre></td></tr></table></figure>
<p>这条命令将文本框输入的文本重定向到age.txt文本中。</p>
<h4 id="msgbox部件"><a href="#msgbox部件" class="headerlink" title="msgbox部件"></a>msgbox部件</h4><p><code>msgbox</code>部件是对话框中最常见的类型。它会在窗口中显示一条简单的信息，直到用户点击OK后消失。</p>
<p>使用格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dialog --msgbox text height width</span><br></pre></td></tr></table></figure>
<p><code>text</code>参数是要在窗口显示的字符串，<code>height</code>与<code>width</code>参数设定自动换行的窗口大小。如果想要在窗口加一个标题，可以使用<code>--title</code>参数，后接作为标题的文本。</p>
<p>例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dialog --title Testing --msgbox "This is a test" 10 20</span><br></pre></td></tr></table></figure>
<p>输入命令后，消息框会显示在终端上。如果你的终端仿真器支持鼠标，可以单击OK来关闭对话框，也可以按下回车键。</p>
<h4 id="yesno部件"><a href="#yesno部件" class="headerlink" title="yesno部件"></a>yesno部件</h4><p>该部件在窗口底部生成两个按钮：一个是Yes，一个是No。用户可以用鼠标、制表符或者键盘方向键来切换按钮。选择按钮则使用空格或者回车键。</p>
<p>下面是一个例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~$ dialog --title "Please answer" --yesno "Is this thing on?" 10 20</span><br><span class="line"><span class="meta">#</span><span class="bash"> 中间终端有输出</span></span><br><span class="line">wsx@wsx:~$ echo $?</span><br><span class="line">0</span><br></pre></td></tr></table></figure>
<p><code>dialog</code>命令的退出状态码会根据用户选择的按钮来设置。选择No返回1，选择Yes就是0。</p>
<h4 id="inputbox部件"><a href="#inputbox部件" class="headerlink" title="inputbox部件"></a>inputbox部件</h4><p>inputbox部件提供了一个简单的文本框区域来输入文本字符串，dialog会将它传到STDERR，我们需要重定向获得输入。inputbox提供了两个按钮：OK和Cancel。如果选择了OK，命令退出状态码为0，否则为1。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ dialog --inputbox "Enter your age:" 10 20 2&gt;age.txt</span><br><span class="line">wsx@wsx:~/tmp$ cat age.txt</span><br><span class="line">24</span><br></pre></td></tr></table></figure>
<p>如果你自己运行过的话就会注意到该值后面没有换行符，这让我们能够轻松将文本内容重定向到脚本变量中，以获得用户输入的值。</p>
<h4 id="textbox部件"><a href="#textbox部件" class="headerlink" title="textbox部件"></a>textbox部件</h4><p>textbox部件是在窗口中显示大量信息的极佳办法。它会生成一个滚动窗口来显示由参数所指定的文件中的文本。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ dialog --textbox /etc/passwd 15 45</span><br></pre></td></tr></table></figure>
<p><code>/etc/passwd</code>文件内容显示在可滚动的文本窗口中，可以用方向键来左右或上下滚动显示文件的内容。窗口底部的行会显示当前查看文本处于文件中的哪个位置（百分比）。文本框只包含一个用来选择退出部件的Exit按钮。</p>
<h4 id="menu部件"><a href="#menu部件" class="headerlink" title="menu部件"></a>menu部件</h4><p>我们可以用这个部件来创建之前（上一篇笔记）中制作的文本菜单的窗口版本。只要为每个选项提供一个选择标号和文本就行。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 4 "Exit" 2&gt;test.txt</span><br></pre></td></tr></table></figure>
<p><strong>第一个参数定义了菜单的标题，之后的两个参数定义了菜单窗口的高和宽，而第四个参数则定义了在窗口中一次显示的菜单项总数。如果存在更多选择，则有滚动条。</strong></p>
<p>菜单项必须使用菜单对：第一个元素是用来选择菜单项的标号（必须唯一）；第二个元素是菜单中使用的文本。</p>
<p>dialog命令会将选定（鼠标点击或回车键或选择OK）的菜单项文本发送到STDERR。</p>
<h4 id="fselect部件"><a href="#fselect部件" class="headerlink" title="fselect部件"></a>fselect部件</h4><p>该部件在处理文件名时非常方便。不用强制用户键入文件名，我们就可以用<code>fselect</code>部件来浏览文件的位置并选择文件。</p>
<p>使用格式：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx:~/tmp$ dialog --title "Select a file" --fselect $HOME/ 10 50 2&gt;file.txt</span><br></pre></td></tr></table></figure>
<p><strong>第一个参数是窗口使用的其实目录位置。<code>fselect</code>部件窗口由左侧的目录列表、右侧的文件列表和含有当前选定的文件或目录的简单文本框组成。可以手动在文本框键入文件名，或者用目录和文件列表来选定（使用空格键选定）</strong>。</p>
<h3 id="dialog选项"><a href="#dialog选项" class="headerlink" title="dialog选项"></a>dialog选项</h3><p>除了标准部件，dialog还有大量定制的选项。前面我们使用的<code>title</code>就是一个。</p>
<p>下面显示了命令可用的选项：</p>
<table>
<thead>
<tr>
<th>选项</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>–add-widget</td>
<td>继续下一个对话框直到按下Esc或Cancel选项</td>
</tr>
<tr>
<td>–aspect ratio</td>
<td>直到窗口宽度和高度的宽高比</td>
</tr>
<tr>
<td>–backtitle title</td>
<td>直到显示在屏幕顶部背景上的图标</td>
</tr>
<tr>
<td>–begin x y</td>
<td>指定窗口左上角的起始位置</td>
</tr>
<tr>
<td>–cancel-label label</td>
<td>指定Cancel按钮的替代标签</td>
</tr>
<tr>
<td>–clear</td>
<td>用默认的对话背景色来清空屏幕内容</td>
</tr>
<tr>
<td>–colors</td>
<td>在对话文本中嵌入ANSI色彩编码</td>
</tr>
<tr>
<td>–cr-wrap</td>
<td>在对话文本中允许使用换行符并强制换行</td>
</tr>
<tr>
<td>–create-rc file</td>
<td>将示例配置文件的内容复制到指定的file文件中</td>
</tr>
<tr>
<td>–defaultno</td>
<td>将yes/no对话框的默认答案设为no</td>
</tr>
<tr>
<td>–default-item string</td>
<td>设定复选列表、表单或菜单对话中的默认项</td>
</tr>
<tr>
<td>–exit-label label</td>
<td>指定Exit按钮的替代标签</td>
</tr>
<tr>
<td>–extra-button</td>
<td>在OK按钮和Cancel按钮之间显示一个额外按钮</td>
</tr>
<tr>
<td>–extra-label label</td>
<td>指定额外按钮的替换标签</td>
</tr>
<tr>
<td>–help</td>
<td>显示dialog命令的帮助信息</td>
</tr>
<tr>
<td>–help-button</td>
<td>在OK按钮和Cancel按钮后显示一个Help按钮</td>
</tr>
<tr>
<td>–help-label label</td>
<td>指定Help按钮的替换标签</td>
</tr>
<tr>
<td>–help-status</td>
<td>当选定Help按钮后，在帮助信息后写入多选列表、单选列表或表单信息</td>
</tr>
<tr>
<td>–ignore</td>
<td>忽略dialog不能识别的选项</td>
</tr>
<tr>
<td>–input-fd fd</td>
<td>指定STDIN之外的另一个文件描述符</td>
</tr>
<tr>
<td>–insecure</td>
<td>在passwd部件中键入内容时显示星号</td>
</tr>
<tr>
<td>–item-help</td>
<td>为多选列表、单选列表或菜单中的每个标号在屏幕底部添加一个帮助栏</td>
</tr>
<tr>
<td>–keep-window</td>
<td>不要清除屏幕上显示过的部件</td>
</tr>
<tr>
<td>–max-input size</td>
<td>指定输入的最大字符串长度。默认为2048</td>
</tr>
<tr>
<td>–nocancel</td>
<td>隐藏Cancel按钮</td>
</tr>
<tr>
<td>–no-collapse</td>
<td>不要将对话文本中的制表符转换为空格</td>
</tr>
<tr>
<td>–no-kill</td>
<td>将tailboxbg对话放到后台，并禁止该进程的SIGHUP信号</td>
</tr>
<tr>
<td>–no-label label</td>
<td>为No按钮指定替换标签</td>
</tr>
<tr>
<td>–no-shadow</td>
<td>不要显示对话窗口的阴影效果</td>
</tr>
<tr>
<td>–ok-label label</td>
<td>指定OK按钮的替换标签</td>
</tr>
<tr>
<td>–output-fd fd</td>
<td>指定除STDERR之外的另一个输出文件描述符</td>
</tr>
<tr>
<td>–print-maxsize</td>
<td>将对话窗口的最大尺寸打印到输出中</td>
</tr>
<tr>
<td>–print-size</td>
<td>将每个对话窗口的大小打印到输出中</td>
</tr>
<tr>
<td>–print-version</td>
<td>将dialog的版本号打印到输出中</td>
</tr>
<tr>
<td>–separate-output</td>
<td>一次一行地输出checklist部件的结果，不使用引号</td>
</tr>
<tr>
<td>–separator string</td>
<td>指定用于分隔部件输出的字符串</td>
</tr>
<tr>
<td>–separate-widget string</td>
<td>指定用于分隔部件输出的字符串</td>
</tr>
<tr>
<td>–shadow</td>
<td>在每个窗口右下角绘制阴影</td>
</tr>
<tr>
<td>–single-quoted</td>
<td>需要时对多选列表的输出采用单引号</td>
</tr>
<tr>
<td>–sleep sec</td>
<td>在处理完对话窗口后延迟指定的秒数</td>
</tr>
<tr>
<td>–stderr</td>
<td>将输出发送到STDERR（默认）</td>
</tr>
<tr>
<td>–stdout</td>
<td>将输出发送到STDOUT</td>
</tr>
<tr>
<td>–tab-correct</td>
<td>将制表符转换为空格</td>
</tr>
<tr>
<td>–tab-len n</td>
<td>指定一个制表符占用的空格数（默认为8）</td>
</tr>
<tr>
<td>–timeout sec</td>
<td>指定无用户输入时，sec秒后退出并返回错误代码</td>
</tr>
<tr>
<td>–title title</td>
<td>指定对话窗口的标题</td>
</tr>
<tr>
<td>–trim</td>
<td>从对话文本中删除前导空格和换行符</td>
</tr>
<tr>
<td>–visit-tiems</td>
<td>修改对话窗口制表符的停留位置，使其包括选项列表</td>
</tr>
<tr>
<td>–yes-label label</td>
<td>为Yes按钮指定替换标签</td>
</tr>
</tbody>
</table>
<p> <strong><code>--backtitle</code>选项是为脚本中的菜单创建公共标题的简便办法。</strong>上表提供的强大特性允许我们创建任何需要的窗口。</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dialog命令支持运行时配置。该命令会根据配置文件模板创建一份配置文件。dialog启动时会先去检查是否设置了DIALOGRC环境变量，该变量会保存配置文件名信息。如果未设置该变量或未找到该文件，它会将<span class="variable">$HOME</span><span class="regexp">/.dialogrc作为配置文件。如果这个文件还不存在的话就尝试查找编译时指定的GLOBALRC文件，也就是/</span>etc<span class="regexp">/dialogrc。如果还不存在就用编译时的默认值。</span></span><br></pre></td></tr></table></figure>
<h3 id="在脚本中使用dialog命令"><a href="#在脚本中使用dialog命令" class="headerlink" title="在脚本中使用dialog命令"></a>在脚本中使用dialog命令</h3><p><strong>必须记住两件事：</strong></p>
<ul>
<li>如果有Cancel或No按钮，检查dialog命令的退出状态码</li>
<li>重定向STDERR来获得输出值</li>
</ul>
<p>接下来是一个简单的实例，使用dialog部件生成我们之前（上一篇笔记）所创建的系统管理菜单。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-laptop:~$ cat menu3</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> using dialog to create a menu</span></span><br><span class="line"></span><br><span class="line">temp=$(mktemp -t test.XXXXXX)</span><br><span class="line">temp2=$(mktemp -t test2.XXXXXX)</span><br><span class="line"></span><br><span class="line">function diskspace &#123;</span><br><span class="line">        df -k &gt; $temp</span><br><span class="line">        dialog --textbox $temp 20 60</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function whoseon &#123;</span><br><span class="line">        who &gt; $temp</span><br><span class="line">        dialog --textbox $temp 20 50</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function menusage &#123;</span><br><span class="line">        cat /proc/meminfo &gt; $temp</span><br><span class="line">        dialog --textbox $temp 20 50</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">while [ 1 ]</span><br><span class="line">do</span><br><span class="line">dialog --menu "Sys Admin Menu" 20 30 10 1 "Display disk space" 2 "Display users" 3 "Display memory usage" 0 "Exit" 2&gt; $temp2</span><br><span class="line">if [ $? -eq 1 ]</span><br><span class="line">then</span><br><span class="line">  break</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">selection=$(cat $temp2)</span><br><span class="line"></span><br><span class="line">case $selection in</span><br><span class="line">1)</span><br><span class="line">        diskspace ;;</span><br><span class="line">2)</span><br><span class="line">        whoseon ;;</span><br><span class="line">3)</span><br><span class="line">        memusage ;;</span><br><span class="line">0)</span><br><span class="line">        break ;;</span><br><span class="line">*)</span><br><span class="line">        dialog --msgbox "Sorry, invalid selection" 10 30</span><br><span class="line">esac</span><br><span class="line">done</span><br><span class="line">rm -f $temp 2&gt; /dev/null</span><br><span class="line">rm -f $temp 2&gt; /dev/null</span><br></pre></td></tr></table></figure>
<p>使用while循环加一个真值常量创建了一个无限循环来显示菜单对话。当执行完每个函数后，脚本会返回继续显示菜单。</p>
<p>脚本使用了mktemp命令创建两个临时文件来保存dialog命令的数据。</p>
<p>后面本来还有图形环境中（KDE、GNOME）使用的一些流行包介绍。以后等我使用ubuntu写笔记时再作学习和介绍吧。</p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="">使用shell创建文本菜单和窗口部件</a></p>
        <!-- modify author information
        <p><span>文章作者:</span><a href="/" title="回到主页">王诗翔</a></p>
        -->
        <p><span>文章作者:</span>王诗翔</p>
        <p><span>发布时间:</span>2017-11-29, 00:00:00</p>
        <p><span>最后更新:</span>2018-01-27, 12:08:47</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="" title="使用shell创建文本菜单和窗口部件">https://shixiangwang.github.io/2017/11/29/shell-create-text-menu-and-window/</a>
            <span class="copy-path" data-clipboard-text="原文: https://shixiangwang.github.io/2017/11/29/shell-create-text-menu-and-window/　　作者: 王诗翔" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="../../../12/08/Git-basic-operation/">
                    学习git
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="../../26/shell-create-function/">
                    创建和使用shell函数
                </a>
            </div>
        
    </nav>


  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#创建文本菜单"><span class="toc-number">1.</span> <span class="toc-text">创建文本菜单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#创建菜单布局"><span class="toc-number">1.1.</span> <span class="toc-text">创建菜单布局</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建菜单函数"><span class="toc-number">1.2.</span> <span class="toc-text">创建菜单函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#添加菜单逻辑"><span class="toc-number">1.3.</span> <span class="toc-text">添加菜单逻辑</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#整合shell脚本菜单"><span class="toc-number">1.4.</span> <span class="toc-text">整合shell脚本菜单</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用select命令"><span class="toc-number">1.5.</span> <span class="toc-text">使用select命令</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#制作窗口"><span class="toc-number">2.</span> <span class="toc-text">制作窗口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#dialog包"><span class="toc-number">2.1.</span> <span class="toc-text">dialog包</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#msgbox部件"><span class="toc-number">2.1.1.</span> <span class="toc-text">msgbox部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#yesno部件"><span class="toc-number">2.1.2.</span> <span class="toc-text">yesno部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#inputbox部件"><span class="toc-number">2.1.3.</span> <span class="toc-text">inputbox部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#textbox部件"><span class="toc-number">2.1.4.</span> <span class="toc-text">textbox部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#menu部件"><span class="toc-number">2.1.5.</span> <span class="toc-text">menu部件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#fselect部件"><span class="toc-number">2.1.6.</span> <span class="toc-text">fselect部件</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#dialog选项"><span class="toc-number">2.2.</span> <span class="toc-text">dialog选项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在脚本中使用dialog命令"><span class="toc-number">2.3.</span> <span class="toc-text">在脚本中使用dialog命令</span></a></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"使用shell创建文本菜单和窗口部件　| Cloudberry　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
        <section id="comments">
    <style> aside.comment-bar { margin: auto 30px; }</style>
    <div id="disqus_thread"></div>
    <script>
        var disqus_config = function(){
            this.page.url = 'https://shixiangwang.github.io/2017/11/29/shell-create-text-menu-and-window/';
            this.page.identifier = '2017/11/29/shell-create-text-menu-and-window/';
        };
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = '//Cloudberry.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        }
    </script>
    
    <script> loadComment(); </script>

</section>


    




    <div class="scroll" id="post-nav-button">
        
            <a href="../../../12/08/Git-basic-operation/" title="上一篇: 学习git">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="../../26/shell-create-function/" title="下一篇: 创建和使用shell函数">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/26/python-basics/">两天研习Python基础</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/12/how-to-create-an-independent-figpath/">为markdown博文创建独立图片路径</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/10/easy-sapply-apply-vapply/">简单理解lapply,sapply,vapply</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/07/how-to-do-group-survival-analysis/">怎么对连续变量分组并进行生存分析</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/06/how-to-write-rmd-documents-in-hexo-system/">怎么在hexo博客系统中用Rmarkdown写文章</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/01/31/sed-how-to-filter-rows-before-using-command/">sed如何在执行命令前过滤特定文本行</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/01/31/sync-deploy-tools/">Sync deploy 命令工具</a></li><li class="post-list-item"><a class="post-list-link" href="../../../12/25/sed-and-gawk/">初识sed与awk</a></li><li class="post-list-item"><a class="post-list-link" href="../../../12/08/Git-basic-operation/">学习git</a></li><li class="post-list-item"><a class="post-list-link" href="">使用shell创建文本菜单和窗口部件</a></li><li class="post-list-item"><a class="post-list-link" href="../../26/shell-create-function/">创建和使用shell函数</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/20/OOPinR/">R-面向对象编程</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/15/Class-in-R/">R中的属性与类</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/04/control_shell/">控制脚本</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/03/Linux-data-analysis-tools/">Linux数据处理命令工具</a></li><li class="post-list-item"><a class="post-list-link" href="../../../08/21/shell-show-data/">Linux脚本编程——呈现数据</a></li><li class="post-list-item"><a class="post-list-link" href="../../../08/19/working-with-user-input/">Shell脚本之处理用户输入</a></li><li class="post-list-item"><a class="post-list-link" href="../../../08/11/structural-command-of-shell/">Linux结构化命令</a></li><li class="post-list-item"><a class="post-list-link" href="../../../08/11/basic-shell/">构建基本shell脚本</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2017-2018 王诗翔
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
    <script src="../../../../js/GithubRepoWidget.js"></script>

<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 2;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
             github: ".github-widget a", 
            
             post: ".article-entry a[href], .copyright a[href]", 
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

    <script>
        var originTitle = document.title;
        var titleTime;
        document.addEventListener("visibilitychange", function() {
            if (document.hidden) {
                document.title = "(つェ⊂) 我藏好了哦~ " + originTitle;
                clearTimeout(titleTime);
            }
            else {
                document.title = "(*´∇｀*) 被你发现啦~ " + originTitle;
                titleTime = setTimeout(function() {
                    document.title = originTitle;
                }, 2000);
            }
        })
    </script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>